<template>
	<view class="login">
		<view class="iconfont icon-gerenzhongxin"></view>
		<view @click="login" class="loginBtn"><text>微信一键登录</text></view>
		<view @click="phone" class="phone"><text>手机号登录</text></view>
	</view>
</template>

<script setup>
import { useLoginStore } from '@/store/loginStore'
import { getSessioId } from '@/api/loginApi.js'
import { onBeforeMount } from 'vue'
onBeforeMount(() => {
	const id = uni.getStorageSync('userId')
	const toked = uni.getStorageSync('token')
	const nikeName = uni.getStorageSync('nikeName')
	const phone = uni.getStorageSync('phone')
	console.log(id, toked)
	if (id && toked) {
		console.log(loginStore)
		loginStore.id = id
		loginStore.toked = toked
		loginStore.nikeName = nikeName
		loginStore.phone = phone
		uni.switchTab({
			url: '/pages/index/index'
		})
	}
})
const loginStore = useLoginStore()

let login = () => {
	console.log('点击登录')
	uni.login({
		success: res => {
			let { code } = res
			getSessioId(code).then(res1 => {
				console.log(res1)
				uni.getUserProfile({
					desc: '获取用户信息',
					success(res2) {
						login(res2)
					}
				})
			})
			// uni.showLoading({
			// 	title: '登陆中'
			// })
			console.log(code)
			// setTimeout(function() {
			// 	uni.switchTab({
			// 		url: '/pages/index/index'
			// 	})
			// }, 2000)
		},
		fail: err => {
			console.log('报错', err)
		}
	})
}
const phone = () => {
	uni.navigateTo({
		url: '/pages/login/PhoneLogin'
	})
}
</script>

<style lang="less" scoped>
.login {
	display: flex;
	align-items: center;
	flex-direction: column;

	.icon-gerenzhongxin {
		font-size: 176rpx;
		margin: 100rpx 0;
		color: #006eff;
	}

	.loginBtn {
		color: #fff;
		background: #006eff;
		width: 686rpx;
		height: 80rpx;
		border-radius: 4px;
		line-height: 80rpx;
		text-align: center;
	}
	.phone {
		.loginBtn;
		background: gray;
		margin-top: 30rpx;
	}
}
</style>
